package com.example.spletnatrgovina;

import java.io.InputStream;
import java.net.URL;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.entity.BufferedHttpEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONObject;

import com.example.spletnatrgovina.ProduktActivity.CustomList;
import com.example.spletnatrgovina.model.Produkt;

import android.app.Activity;
import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {
	public static final String URL_PREFIX = "http://10.0.2.2/spletna";
	private ImageView img;

	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		new GetImageAsync(getApplicationContext()).execute();

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}
	
	public void enter(View v) {
        Intent intent = new Intent(this,ProduktActivity.class);
		startActivity(intent);
	}
	
	private class GetImageAsync extends AsyncTask<Void, Void, Bitmap> {
		private final Context context;
	
		public GetImageAsync(Context c) {
			context = c;
		}
	
		@Override
		protected Bitmap doInBackground(Void... params) {
			Bitmap bitmap = null;
			try {
			        URL url = new URL(URL_PREFIX+"/media/mobile_devices.jpg");
			        HttpGet httpRequest = null;
			        httpRequest = new HttpGet(url.toURI());
			        HttpClient httpclient = new DefaultHttpClient();
			        HttpResponse response = (HttpResponse) httpclient.execute(httpRequest);
			        HttpEntity entity = response.getEntity();

			        BufferedHttpEntity b_entity = new BufferedHttpEntity(entity);
			        InputStream input = b_entity.getContent();
			        bitmap = BitmapFactory.decodeStream(input);

			} catch (Exception ex) {
                    Log.e("image url", "msg: "+ex.getMessage());
			}
	
			return bitmap;
		}
	
		@Override
		protected void onPostExecute(Bitmap image) {
			if (image == null) {
				Toast.makeText(context, "An error occured.", Toast.LENGTH_LONG).show();
			} else {
				img = (ImageView) findViewById(R.id.img);
                img.setImageBitmap(image);
				
			}
		}
		
	}
	
}
